package com.canal.admin.service.impl;

import com.canal.admin.dao.ZcolBusRecordMapper;
import com.canal.admin.dao.ZcolStDataMapper;
import com.canal.admin.service.HistoryDataService;
import com.canal.admin.transfer.req.data.CanalHiConditions;
import com.canal.admin.transfer.req.data.HistoryDataConditions;
import com.canal.admin.transfer.resp.CanalHiResp;
import com.canal.admin.transfer.resp.PageResp;
import com.canal.admin.transfer.resp.rt.RtConvertResp;
import com.canal.admin.util.PageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yangbo
 * @date 2020-07-22 21:24
 * @description
 * @since
 */
@Service
@Slf4j
public class HistoryDataServiceImpl implements HistoryDataService {

    @Autowired
    private ZcolStDataMapper zcolStDataMapper;

    @Autowired
    private ZcolBusRecordMapper zcolBusRecordMapper;

    @Override
    public PageResp historyList(HistoryDataConditions conditions) {
        PageHelper.startPage(conditions.getPageNo(),conditions.getPageSize());
        List<RtConvertResp> stDataRespList = zcolStDataMapper.historyList(conditions);
        PageInfo<RtConvertResp> pageInfo = new PageInfo<>(stDataRespList);
        PageResp pageResp = PageUtil.toResp(pageInfo);
        List<RtConvertResp> respList = pageResp.getDataList();
        List<RtConvertResp> resultList = new ArrayList<>(respList.size() + 1);
        for (RtConvertResp convert : respList) {
            BigDecimal regRate = new BigDecimal(convert.getRegCount())
                    .divide(new BigDecimal(convert.getUv()),4, RoundingMode.HALF_DOWN)
                    .multiply(new BigDecimal(100)).setScale(2,RoundingMode.HALF_DOWN);
            BigDecimal loginRate;
            if(convert.getRegCount() > 0) {
                loginRate = new BigDecimal(convert.getLoginCount())
                        .divide(new BigDecimal(convert.getRegCount()),4,RoundingMode.HALF_DOWN)
                        .multiply(new BigDecimal(100)).setScale(2,RoundingMode.HALF_DOWN);
            } else {
                loginRate = BigDecimal.ZERO;
            }
            convert.setRegRate(regRate);
            convert.setLoginRate(loginRate);
            convert.setCanalUv(convert.getCanalUv());
            convert.setIncomeAmount(convert.getIncomeAmount());
            BigDecimal avgUv = new BigDecimal(convert.getCanalUv())
                    .divide(new BigDecimal(convert.getUv()),4,RoundingMode.HALF_DOWN);
            convert.setAvgUv(avgUv);
            BigDecimal profit = convert.getIncomeAmount().subtract(convert.getRegCostAmount());
            convert.setProfit(profit);
            resultList.add(convert);
        }
        pageResp.setDataList(resultList);
        return pageResp;
    }

    @Override
    public PageResp canalHistoryList(CanalHiConditions conditions) {
        PageHelper.startPage(conditions.getPageNo(),conditions.getPageSize());
        List<CanalHiResp> canalHiRespList = zcolBusRecordMapper.canalHiList(conditions);
        PageInfo<CanalHiResp> pageInfo = new PageInfo<>(canalHiRespList);
        PageResp pageResp = PageUtil.toResp(pageInfo);
        return pageResp;
    }
}
